package com.c.ctgproject.ui.collect

import android.content.Intent
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryOutSubmitBinding
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述:电池批量出库 页面
 */
class BatteryOutSubmitActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryOutSubmitBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val pbIds = ArrayList<Int>()
    private val selectList by lazy { intent.getSerializableExtra(MyConstants.DATA) as ArrayList<BatteryCanHandlerEntity.DataBean.ListBean> }
    override fun event() {
        //模组数量
        var moduleCount = 0
        //电芯数量
        var coreCount = 0
        //电池包数量
        var count = 0
        selectList.forEach { bean ->
            if (bean.batteryType == 1 ) {
                bean.count?.let {
                    count += it
                }
            }

            if (bean.batteryType == 2) {
                bean.moduleCount?.let {
                    moduleCount += it
                }
            }
            if (bean.batteryType == 3) {
                bean.moduleCount?.let {
                    coreCount += it
                }
            }
            pbIds.add(bean.pbId)
        }
        val text = "合计:${count}个电池包,${moduleCount}个模组,${coreCount}个电芯"
        mBinding.mPartsTV.text = text
    }
    var  customerName=""
    //出库类型 1销售出库 2借用出库 3其他出库
    private var outType = 1
    override fun click() {
        mBinding.mXS.setOnClickListener { outType = 1 }
        mBinding.mJY.setOnClickListener { outType = 2 }
        mBinding.mQT.setOnClickListener { outType = 3 }

        //选择客户名称
        mBinding.mDictionarySelectLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.CUSTOMER.menuCode, true, arrayListOf(customerName))
        }


        mBinding.mConfirmBTN.setOnClickListener {
            if (customerName.isEmpty()) {
                showToast("请选择客户名称")
            } else {
                val map = HashMap<String, Any>()
                map["customer"] = customerName
                map["outType"] = outType
                map["pbIdList"] = pbIds
                map["remark"] = mBinding.mRemarkET.text.handlerNull()
                ApiMethods.apiSubscribe(ApiMethods.retrofit.powerOutSubmit(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("回调:$it")
                    showToast("提交成功")
                    finish()
                })
            }
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let { result ->
            //字典回调
            if (requestCode == 1000) {
                val name = result.getStringExtra(MyConstants.NAME)
                customerName = name.handlerNull()
                mBinding.mClientET.text = name.handlerNull()
            }

        }
    }
}